<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=11,chrome=1">
    <meta charset="utf-8">
    <meta name="description" content="">
    <meta name="keywords" content="">
    <meta name="author" content="">
    <script src="js/jquery.min.js"></script>
    <!–[if IE 9]><script type="text/javascript" src="js/jquery-1.7.2.min.js"></script><![endif]–>
    <!–[if IE 9]><script src="js/jquery.xdomainrequest.min.js"></script><![endif]–>
    <!–[if IE 8]><script src="js/jquery.xdomainrequest.min.js"></script><![endif]–>
    <!–[if IE 8]><script type="text/javascript" src="js/jquery.min.js"></script><![endif]–>
    <script src="js/qrcode.min.js"></script>
    <title></title>   
</head>
<body>
<div><h1 align="center" style="margin: 100px">请用龙城市民云进行扫码</h1></div>
<div id="qrcode2" align="center" style="margin: 200px"></div>
<div align="center"><span id="periodTime" style="color: red">60</span></div>
</body>
<script type="text/javascript">


    /* *
     * 以下参数为配置参数
     */
    var client_id = "dbb8b982ad154f699805914b0a4c79a5";   //测试专用
    var redirect_uri= "http://localhost:8089/oauth2-demo/my.html";  //测试专用

//    var client_id = "c91eac32d1a64118b591061bcb467960";
//    var redirect_uri= "https://10.35.252.230";

    var qrcode = new QRCode(document.getElementById("qrcode2"), {
            width: 250,
            height: 250
        });
        var controller = {};
        //code为generateCode接口生成二维码的唯一标识
        var code = "";
        var timer;
        $(document).ready(function (ev) {
            //cors、ajaxSetup两个都要设置，后台还需要设置所有域名都可访问，'Access-Control-Allow-Origin': '*'
            jQuery.support.cors = true;
            jQuery.ajaxSetup({
                xhr: function() {
                    if(window.ActiveXObject){
                        return new window.ActiveXObject("Microsoft.XMLHTTP");
                    }else{
                        return new window.XMLHttpRequest();
                    }
                }
            });
            console.log("准备进入方法");
            controller.makeCode = function makeCode() {
                console.log("进入方法了。。。");
                $.ajax({
                    type: "GET",
                    url: "https://lcsmy.liuzhou.gov.cn/oauth2/web/generateCode",
                    crossDomain: true == !(document.all),
                    success: function (msg) {
                        console.log(msg);
                        code = msg.data.code;
                        codeWithClientId = code + "&client_id="+client_id;
                        qrcode.makeCode(codeWithClientId);
                        console.log("二维码已生成");
                        timer = setInterval(controller.isLogin, 1000);
                        console.log("执行定时器");
                    }
                });
            };

            controller.isLogin = function isLogin() {
                console.log("定时器里面");
                $.ajax({
                    type: "POST",
                    url: "https://lcsmy.liuzhou.gov.cn/oauth2/web/isSuccess",
                    data: "code=" + code,
                    async: false,
                    success: function (msg) {
                        if (msg.code == 100001) {
                            console.log("controller.isLogin：true");
                        }
                        //该接口不能放到if里面，因为接口需要高频刷新验证
                        controller.login();
                    }
                });
            };

            controller.login = function login() {
                $.ajax({
                    type: "POST",
                    url: "https://lcsmy.liuzhou.gov.cn/oauth2/web/login/validate",
                    data: {
                        "loginType" : 0,
                        "redirect_uri" : redirect_uri,
                        "client_id" : client_id,
                        "code": code,
                    },
                    success: function (result) {
                        var periodTime = $("#periodTime");
                        var time = periodTime.text() -1;
                        periodTime.text(time);
                        console.log(result);
                        if(result.code == 200){
                            //这一步会返回一个有效期为一分钟的code，拼接到回调地址上
                            if ("servlet"==result.data.method){
                                alert(result.data.url)
//                                location.href = result.data.url;
                                var code2 = result.data.url.split("=")[1];
                                console.log(code2);
                                getUserMsg(code2)
                            }
                        }
                    }
                });
            };
            //获取用户信息
            function getUserMsg(code) {
                $.ajax({
                    url: '/oauth2-demo/getUserMsg',
                    method: 'POST',
                    data: {
                        code: code,
                        client_id: client_id,
                        redirect_uri: redirect_uri
                    },
                    success: function (result) {
                        var resultObj = eval('(' + result + ')');
                        var type = resultObj.code;
                        console.log(resultObj);
                        if (type == "200") {
                            var msg = resultObj.data;
                            alert(JSON.stringify(msg));
                            console.log(msg);
//                    loginMsg(msg);
                        }
                    }

                });
            }
            controller.makeCode();

        });
</script>
</html>
